export function pay() {
  const self = this;
  wx.getSetting({
    withSubscriptions: true,
    success(res) {
      var itemSettings = res.subscriptionsSetting.itemSettings;
      if (itemSettings) {
        if (
          itemSettings["Liybyf6Oik96Wgr3DPgxX6DZENh5oK9twbJtHYSsZ3I"] !==
          "accept"
        ) {
          wx.requestSubscribeMessage({
            tmplIds: ["Liybyf6Oik96Wgr3DPgxX6DZENh5oK9twbJtHYSsZ3I"],
            complete(res) {
              payment(self);
            }
          });
        } else {
          payment(self);
        }
      } else {
        wx.requestSubscribeMessage({
          tmplIds: ["Liybyf6Oik96Wgr3DPgxX6DZENh5oK9twbJtHYSsZ3I"],
          complete(res) {
            payment(self);
          }
        });
      }
    }
  });
}

export function payForMember() {
  let self = this;
  self.$store.dispatch("setPaySpinning", true);
  let body = self.$store.state.payInfo;
  body.openId = self.$store.state.user.openId;
  body.shopId = self.$store.state.shop.id;
  self.$http.post({
    url: "mp/payForMember",
    method: "POST",
    body: body
  }).then(res => {
    self.$store.dispatch("setPaySpinning", false);
    if (res.freepay === true) {
      self.$store.dispatch("saveResult", res);
      self.$router.push({ path: "/pages/result", reLaunch: true })
    } else {
      wx.requestPayment({
        timeStamp: res.timeStamp.toString(),
        nonceStr: res.nonceStr,
        package: res.package,
        signType: 'MD5',
        paySign: res.paySign,
        success: (_res) => {
          self.$store.dispatch("saveCheckTransactionId", res.outTradeNo);
          self.$router.push({ path: "/pages/paymentComplete", reLaunch: true })
        },
        fail: function (_res) {
          wx.showToast({
            title: "支付失败!",
            icon: 'none',
            duration: 1500
          })
        },
        complete: function (_res) {
        }
      })
    }
  }).catch(err => {
    self.$store.dispatch("setPaySpinning", false);
    console.log(err);
  })
}

function payment(self) {
  self.$store.dispatch("setPaySpinning", true);
  if (JSON.stringify(self.$store.state.shop) === '{}') {
    wx.showModal({
      title: '提示',
      content: '请先选择门店',
      success: function (res) {
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/shopList'
          })
        }
      }
    });
  }
  self.$http.post({
    url: "mp/pay",
    method: "POST",
    body: {
      roomNum: self.$store.state.user.roomNum,
      realName: self.$store.state.user.realName,
      phone: self.$store.state.user.phone,
      openId: self.$store.state.user.openId,
      totalPrice: self.$store.state.submitGoods.totalPrice,
      shopId: self.$store.state.shop.id,
      shopTitle: self.$store.state.shop.title,
      target: self.$store.state.submitGoods.target,
      cart: JSON.stringify(self.$store.state.submitGoods.goodsNum)
    }
  }).then(res => {
    self.$store.dispatch("setPaySpinning", false);
    if (res.freepay === true) {
      self.$store.dispatch("saveResult", res);
      self.$router.push({ path: "/pages/result", reLaunch: true })
    } else {
      wx.requestPayment({
        timeStamp: res.timeStamp.toString(),
        nonceStr: res.nonceStr,
        package: res.package,
        signType: 'MD5',
        paySign: res.paySign,
        success: (_res) => {
          self.$store.dispatch("saveCheckTransactionId", res.outTradeNo);
          self.$router.push({ path: "/pages/paymentComplete", reLaunch: true })
        },
        fail: function (_res) {
          wx.showToast({
            title: "支付取消!",
            icon: 'none',
            duration: 1500
          })
        },
        complete: function (_res) {
        }
      })
    }
  }).catch(err => {
    self.$store.dispatch("setPaySpinning", false);
    console.log(err);
  })
}

export function payForBorrow() {
  const self = this;
  wx.getSetting({
    withSubscriptions: true,
    success(res) {
      var itemSettings = res.subscriptionsSetting.itemSettings;
      if (itemSettings) {
        if (
          itemSettings["Liybyf6Oik96Wgr3DPgxX6DZENh5oK9twbJtHYSsZ3I"] !==
          "accept"
        ) {
          wx.requestSubscribeMessage({
            tmplIds: ["Liybyf6Oik96Wgr3DPgxX6DZENh5oK9twbJtHYSsZ3I"],
            complete(res) {
              paymentForBorrow(self);
            }
          });
        } else {
          paymentForBorrow(self);
        }
      } else {
        wx.requestSubscribeMessage({
          tmplIds: ["Liybyf6Oik96Wgr3DPgxX6DZENh5oK9twbJtHYSsZ3I"],
          complete(res) {
            paymentForBorrow(self);
          }
        });
      }
    }
  });
}

function paymentForBorrow(self) {
  self.$store.dispatch("setPaySpinning", true);
  if (JSON.stringify(self.$store.state.shop) === '{}') {
    wx.showModal({
      title: '提示',
      content: '请先选择门店',
      success: function (res) {
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/shopList'
          })
        }
      }
    });
  }
  self.$http.post({
    url: "mp/payForBorrow",
    method: "POST",
    body: {
      openId: self.$store.state.user.openId,
      orderId: self.$store.state.borrow.orderId,
      totalPrice: self.$store.state.borrow.payTimeoutPrice
    }
  }).then(res => {
    self.$store.dispatch("setPaySpinning", false);
    if (res.freepay === true) {
      self.$store.dispatch("saveResult", res);
      self.$router.push({ path: "/pages/result", reLaunch: true })
    } else {
      wx.requestPayment({
        timeStamp: res.timeStamp.toString(),
        nonceStr: res.nonceStr,
        package: res.package,
        signType: 'MD5',
        paySign: res.paySign,
        success: (_res) => {
          self.$store.dispatch("saveCheckTransactionId", res.outTradeNo);
          self.$router.push({ path: "/pages/paymentComplete", reLaunch: true })
        },
        fail: function (_res) {
          wx.showToast({
            title: "支付取消!",
            icon: 'none',
            duration: 1500
          })
        },
        complete: function (_res) {
        }
      })
    }
  }).catch(err => {
    console.log(err);
    self.$store.dispatch("setPaySpinning", false);
  })
}